/*    

    bobbietalk - a visual programming environment for toy robotics

    Copyright (C) 2013  Roberto Tiella

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
package bobbietalk.gui.dialogs;

import java.util.regex.Pattern;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.text.JTextComponent;

/**
 *
 * @author roberto
 */
public class IntegerRangeVerifier extends InputVerifier {

    private int min;
    private int max;
    private BlockEditingDialog myDialog;

    public IntegerRangeVerifier(int min, int max, BlockEditingDialog myDialog) {
        this.min = min;
        this.max = max;
        this.myDialog = myDialog;
    }
    
    

    public int getMin() {
        return min;
    }

    public void setMin(int min) {
        this.min = min;
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }
    
    
    
    @Override
    public boolean verify(JComponent input) {
        
        boolean res = false;
        
        JTextComponent textInput = (JTextComponent) input;
        
        String text = textInput.getText(); 
        
        if (!text.isEmpty()) {
        
            res = Pattern.matches("^[0-9]+$", text);
            
            if (res) {
                
                int v = Integer.parseInt(text);
                
                res = min <= v && v <= max;
                
            }
        
        }
        
  
        myDialog.setOkButtonEnabled(res);
        return res;
    }
    
}
